package com.ywjh.inter.servlet.doc;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ywjh.inter.conn.MemcacheConn;
import com.ywjh.inter.entity.ProfileEntity;
import com.ywjh.inter.entity.UsersEntity;
import com.ywjh.inter.entity.UsersHXEntity;
import com.ywjh.inter.service.HXService;
import com.ywjh.inter.service.UsersService;
import com.ywjh.inter.utils.StrUtil;

import net.sf.json.JSONObject;

public class CreatePatient extends HttpServlet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2898640987572132556L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html; charset=utf-8");
		JSONObject data = StrUtil.getPostJSON(req);
		int r = 0;
		String e = "";
		JSONObject json = new JSONObject();
		int doc_id = data.getInt("doc_id");
		String name = data.getString("name");
		String idcard = data.getString("idcard");
		float high = StrUtil.getFloat(data.getString("high"), 0.0f);
		float weight = StrUtil.getFloat(data.getString("weight"), 0.0f);
		int bmi = data.getInt("bmi");
		int default_address_id = data.getInt("default_address_id");
		String memo = data.getString("memo");
		// 疾病分组
		int disease_id = data.getInt("disease_id");
		// 历史疾病
		String his_disease = data.getString("his_disease");
		int gender=data.getInt("gender");
		String brithday=data.getString("brithday");
		String phone = data.getString("phone");
		String group_disease=data.getString("group_disease");
		ProfileEntity entity = new ProfileEntity();
		entity.setDisease_id(disease_id);
		entity.setHigh(high);
		entity.setHis_disease(his_disease);
		entity.setIdcard(idcard);
		entity.setName(name);
		entity.setStatus(1);
		entity.setWeight(weight);
		entity.setAdd_time(new Timestamp(System.currentTimeMillis()));
		entity.setUpdate_time(new Timestamp(System.currentTimeMillis()));
		entity.setBmi(bmi);
		entity.setMemo(memo);
		entity.setDefault_address_id(default_address_id);
		entity.setBrithday(brithday);
		entity.setGender(gender);
		entity.setGroup_disease(group_disease);
		UsersEntity u = new UsersEntity();
		u.setLoginname(phone);
		u.setPassword("123456");
		u.setPhone(phone);
		u.setRegister_time(new Timestamp(System.currentTimeMillis()));
		u.setStatus(1);
		u.setIs_doc(0);
		System.out.println("CreatePatient");
		boolean existPhone = UsersService.getInstance().existPhone(phone);
		if (existPhone) {
			e = "该手机号已经存在";
		} else {
			if (UsersService.getInstance().registerUser(u)>-1) {
				System.out.println("111111");
				u = UsersService.getInstance().findUserByPhone(phone);
				entity.setUser_id(u.getId());
				if (UsersService.getInstance().addProfile(entity)) {
					System.out.println("22222222");
					UsersHXEntity hx_p = UsersService.getInstance().findHXUserByUserId(u.getId());
					UsersHXEntity hx_d = UsersService.getInstance().findHXUserByUserId(doc_id);
					if (hx_d != null) {
						System.out.println("33333333");
						HXService.getInstance().addFriend(hx_d.getUsername(), hx_p.getUsername(),
								HXService.getInstance().token());
						MemcacheConn.getInstance().delete("list_by_disease_doc_" + doc_id);
						r = 1;
						JSONObject d=new JSONObject();
						d.put("user_id", u.getId());
						json.put("d", d);
					} else {
						e = "医生信息不存在";
					}
				} else {
					e = "系统错误";
				}
			} else {
				e = "系统错误";
			}
		}
		json.put("r", r);
		json.put("e", e);
		PrintWriter pw = resp.getWriter();
		pw.write(json.toString());
		pw.flush();
		pw.close();
	}
}
